package tests;
import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import src.Library;
import src.UserAccount;

public class TestLibraryUserHandling {

	private Library objectOfClassUnderTest;

	private UserAccount user;

	@Before
	public void setUp() throws Exception {
		objectOfClassUnderTest = Library.getTheLibrary();

		user = new UserAccount("name", "pi", "name@mail.de");
	}

	@After
	public void tearDown() throws Exception {
		objectOfClassUnderTest = null;
		System.gc();
	}

	@Test
	public final void testAddUserAccountSuccessful() {

		assertTrue(
				"The Librarys add user account method should return true when a user is successfully added!",
				objectOfClassUnderTest.addUserAccount(user));
		assertTrue("A added User should be found at the library.",
				objectOfClassUnderTest.hasUserAccount(user));

	}

	@Test
	public void testAddUserAccountUnsuccessfulBecauseOfSamePi()
			throws Exception {
		UserAccount user2 = new UserAccount("name", "pi", "name@mail.de");
		objectOfClassUnderTest.addUserAccount(user);
		assertFalse(
				"It should not be possible to add two users with the same person identifier to the library!",
				this.objectOfClassUnderTest.addUserAccount(user2));
	}

	@Test
	public void testAddUserAccountWithNullArgument() throws Exception {
		try {
			objectOfClassUnderTest.addUserAccount(null);
			fail("A NullPointerException should be thrown!");
		} catch (NullPointerException exception) {

		}
	}

	@Test
	public final void testRemoveUserAccountSuccessful() {
		objectOfClassUnderTest.addUserAccount(user);
		assertTrue(
				"It should be possible to remove a user successfully added to the library!",
				objectOfClassUnderTest.removeUserAccount(user));
	}

	@Test
	public void testRemoveUserAccountUnsuccessful() throws Exception {
		assertFalse(
				"The mehtod removeUserAccount of the library should return false if the user is not registered!",
				objectOfClassUnderTest.removeUserAccount(user));
	}

	@Test
	public void testRemoveUserAccountWithNullArgument() throws Exception {
		try {
			objectOfClassUnderTest.removeUserAccount(null);
			fail("A NullPointerException should be thrown!");
		} catch (NullPointerException exception) {

		}
	}

	@Test
	public final void testHasUserAccount() {
		objectOfClassUnderTest.addUserAccount(user);

		assertTrue("In Library registered user was not returned!",
				objectOfClassUnderTest.hasUserAccount(user));
	}

	@Test
	public void testHasUserAccountWithNullArgument() throws Exception {
		try {
			objectOfClassUnderTest.hasUserAccount(null);
			fail("A NullPointerException should be thrown!");
		} catch (NullPointerException exception) {

		}
	}

	@Test
	public void testHasUserAccountWithNotContainedUser() throws Exception {
		assertFalse("User should not have been in the library!",
				objectOfClassUnderTest
						.hasUserAccount(new UserAccount("somenotcontaineduser",
								"somerandompi", "some@mail.de")));
	}

}
